using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Masood.MobileGames.SlidingLib.Abstract;
using System.Windows.Media.Imaging;


namespace Masood.MobileGames.SlidingLib.Common
{
    public class Helper
    {
        private static Helper instance = null;
        private List<Color> colors;
        private List<Color> fontColors;
        private Random random;

        private Helper()
        {
            colors = new List<Color>();
            fontColors = new List<Color>();
            FillColors();
            random = new Random();
        }

        private void FillColors()
        {
            //colors.Add(Color.AliceBlue);
            colors.Add(Color.DarkOliveGreen); fontColors.Add(Color.White);
            colors.Add(Color.DarkOrange); fontColors.Add(Color.White);
            colors.Add(Color.Orange); fontColors.Add(Color.White);
            colors.Add(Color.Red); fontColors.Add(Color.White);
            colors.Add(Color.Purple); fontColors.Add(Color.White);
            colors.Add(Color.Blue); fontColors.Add(Color.White);
            colors.Add(Color.Yellow); fontColors.Add(Color.Green);
            colors.Add(Color.YellowGreen); fontColors.Add(Color.White);
            colors.Add(Color.Green); fontColors.Add(Color.White);
            colors.Add(Color.Gold); fontColors.Add(Color.White);
            colors.Add(Color.HotPink); fontColors.Add(Color.Black);
            colors.Add(Color.White); fontColors.Add(Color.Green);
        }

        public static Helper GetInstance()
        {
            if (instance == null)
                instance = new Helper();

            return instance;
        }

        public BoxAppearanaceInfo GetNextAppearnace()
        {
            int index = random.Next(colors.Count);
            BoxAppearanaceInfo info = new BoxAppearanaceInfo();

            info.FilledColor = colors[index];
            info.FontColor = fontColors[index];

            return info;
        }

        public Color GetNextColorForWhiteFont()
        {
            return colors[random.Next(colors.Count)];
        }

        public static Texture2D ImageStreamToTexture2D(GraphicsDevice GraphicsDevice, System.IO.Stream imageStream)
        {   
            // Creates a texture from IO.Stream - our memory stream   
            Texture2D texture = Texture2D.FromStream(GraphicsDevice, imageStream);
            
            return texture;
        }

        public static Texture2D[] Split(Texture2D original, int partWidth, int partHeight, out int xCount, out int yCount)
        {
            xCount = original.Height / partHeight +(partHeight % original.Height == 0 ? 0 : 1);//The number of textures in each horizontal row
            yCount = original.Width / partWidth + (partWidth % original.Width == 0 ? 0 : 1);//The number of textures in each vertical column
            Texture2D[] r = new Texture2D[xCount * yCount];//Number of parts = (area of original) / (area of each part).
            int dataPerPart = partWidth * partHeight;//Number of pixels in each of the split parts

            //Get the pixel data from the original texture:
            Color[] originalData = new Color[original.Width * original.Height];
            original.GetData<Color>(originalData);

            int index = 0;
            //for (int y = 0; y < yCount * partHeight; y += partHeight)
            for (int x = 0; x < xCount * partWidth; x += partWidth)
                //for (int x = 0; x < xCount * partWidth; x += partWidth)
                for (int y = 0; y < yCount * partHeight; y += partHeight)
                {
                    //The texture at coordinate {x, y} from the top-left of the original texture
                    Texture2D part = new Texture2D(original.GraphicsDevice, partWidth, partHeight);
                    //The data for part
                    Color[] partData = new Color[dataPerPart];

                    //Fill the part data with colors from the original texture
                    for (int py = 0; py < partHeight; py++)
                        for (int px = 0; px < partWidth; px++)
                        {
                            int partIndex = px + py * partWidth;
                            //If a part goes outside of the source texture, then fill the overlapping part with Color.Transparent
                            if (y + py >= original.Height || x + px >= original.Width)
                                partData[partIndex] = Color.Transparent;
                            else
                                partData[partIndex] = originalData[(x + px) + (y + py) * original.Width];
                        }

                    //Fill the part with the extracted data
                    part.SetData<Color>(partData);
                    //Stick the part in the return array:                    
                    r[index++] = part;
                }
            //Return the array of parts.
            return r;
        }

    }
}
